<?php

namespace App\Http\Controllers\Business;

use App\Common\Traits\Model\ModelSearchTrait;
use App\Http\Controllers\Controller;
use App\Models\Business\ShopProfileTeam;
use Illuminate\Http\Request;

class ShopProfileTeamController extends Controller
{
    use ModelSearchTrait;

    //
    public function index(Request $request, ShopProfileTeam $profileTeam)
    {
        $profileTeam=$this->singleModelSearch($request,$profileTeam,$this->teamCondition());
        $settings=$profileTeam->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($settings);
    }

    //
    public function teamCondition()
    {
        return [
            ['team_name','like'],
        ];
    }

    //
    public function store(Request $request)
    {
        if(!$request->filled('team_name')){
            return $this->failed('模板信息分组名称不能为空',201);
        }
        $data=['team_name'=>mb_substr($request->team_name,0,64)];
        ShopProfileTeam::create($data);
        return $this->success(['msg'=>'添加成功']);
    }

    //
    public function show(Request $request)
    {
        $show=ShopProfileTeam::find($request->team_id??0);
        return $this->success($show);
    }

    //
    public function update(Request $request, ShopProfileTeam $profileTeam)
    {
        if(!$request->filled('team_name')){
            return $this->failed('模板信息分组名称不能为空',201);
        }
        $team=$profileTeam->find($request->team_id??0);
        if(empty($team)){
            return $this->failed('id错误，无此分组id',201);
        }
        $team->team_name=mb_substr($request->team_name,0,64);
        $team->save();
        return $this->success(['msg'=>'编辑成功']);
    }

    //
    public function destroy(Request $request, ShopProfileTeam $profileTeam)
    {
        $team=$profileTeam->find($request->team_id??0);
        if(empty($team)){
            return $this->failed('id错误，无此分组id',201);
        }
        $team->delete();
        return $this->success(['msg'=>'删除成功']);
    }

    //
    public function getAllTeams(){
        $teams=ShopProfileTeam::all();
        return $this->success($teams);
    }


}
